home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / apps.to.go / AppWannabe / Menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-11  |  9.5 KB  |  359 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Menu.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.defs.h"        /* Get various application definitions.            */
  27. #include "App.protos.h"        /* Get the prototypes for application.            */
  28.  
  29. #ifndef __DESK__
  30. #include <Desk.h>
  31. #endif
  32.  
  33. #ifndef __ERRORS__
  34. #include <Errors.h>
  35. #endif
  36.  
  37. #ifndef __MEMORY__
  38. #include <Memory.h>
  39. #endif
  40.  
  41. #ifndef __MENUS__
  42. #include <Menus.h>
  43. #endif
  44.  
  45. #ifndef __TOOLUTILS__
  46. #include <ToolUtils.h>
  47. #endif
  48.  
  49. #ifndef __UTILITIES__
  50. #include "Utilities.h"
  51. #endif
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. extern Boolean    gQuitApplication;
  60. extern Boolean    gHasAppleEvents;
  61. extern OSType    gAppWindowType;
  62.  
  63. extern Boolean    gLowOnMem;
  64. extern short    gDialogErr;
  65.  
  66.  
  67.  
  68. /*****************************************************************************/
  69. /*****************************************************************************/
  70.  
  71.  
  72.  
  73. /* •• Called by DTS.Lib..framework. •• */
  74.  
  75. /* Adjust the menu items.  We allow the DTS.Lib framework to do most of the work
  76. ** for us.  It will walk the menubar, and for each menu in the menubar, it will
  77. ** disable all of the menu items and then call the application at either
  78. ** AdjustMenuItems() (for document and palette windows, or for the no-window case),
  79. ** or DialogAdjustMenuItems() (for modal dialogs).  The application's job is to then
  80. ** turn on menu items that should be enabled to match the current application state.
  81. ** The initial Wannabe code for AdjustMenuItems() calls DoAdjustFileMenu() for the
  82. ** file menu, and DoAdjustEditMenu() for the edit menu.  Any other menus that may
  83. ** be added to Wannabe have all menu items enabled.  This allows menus to be added
  84. ** to the running version of Wannabe and allows them to actually do something.
  85. ** If the top-most window is a dialog, then all menus are disabled except for the
  86. ** edit menu.  Various items in the edit menu are enabled, depending on if there
  87. ** is an active TextEdit control, and what is in the clipboard. */
  88.  
  89. #pragma segment Menu
  90. void    DoAdjustMenus(void)
  91. {
  92.     if (DoAdjustMBARMenus(FrontWindow(), rMenuBar))
  93.         DrawMenuBar();
  94. }
  95.  
  96.  
  97.  
  98. /*****************************************************************************/
  99.  
  100.  
  101.  
  102. /* This is called when an item is chosen from the menu bar (after calling
  103. ** MenuSelect or MenuKey).  It performs the right operation for each command.
  104. ** It is good to have both the result of MenuSelect and MenuKey go to one
  105. ** routine like this to keep everything organized. */
  106.  
  107. #pragma segment Menu
  108. Boolean    DoMenuCommand(short menuID, short menuItem)
  109. {
  110.     short        undoDepth, numUndos, saveMode, daRefNum;
  111.     Str255        daName;
  112.     FileRecHndl    frHndl;
  113.     WindowPtr    window;
  114.     TEHandle    te;
  115.     OSErr        err;
  116.     Boolean        handled;
  117.  
  118.     handled = true;
  119.  
  120.     window = FrontWindow();
  121.     if (window)
  122.         frHndl = (FileRecHndl)GetWRefCon(window);
  123.             /* frHndl is valid only if it is one of our windows. */
  124.  
  125.     switch (menuID) {
  126.  
  127.         case mApple:
  128.             switch (menuItem) {
  129.                 case kStdAbout:        /* Bring up alert for About. */
  130.                     NewDocumentWindow(nil, 'ABOT', false);
  131.                     break;
  132.                 default:            /* All non-About items in this menu are DAs. */
  133.                     GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
  134.                     daRefNum = OpenDeskAcc(daName);
  135.                     break;
  136.             }
  137.             break;
  138.  
  139.         case mFile:
  140.             switch (menuItem) {
  141.                 case kStdNew:
  142.                     gDialogErr = NewDocumentWindow(&frHndl, gAppWindowType, true);
  143.                     if (gDialogErr)
  144.                         NewDocumentWindow(nil, 'ERR#', false);
  145.                     break;
  146.                 case kStdOpen:
  147.                     err = OpenDocumentWindow(&frHndl, nil, fsRdWrPerm);
  148.                     if ((err) && (err != userCanceledErr)) {
  149.                         gDialogErr = err;
  150.                         NewDocumentWindow(nil, 'ERR#', false);
  151.                     }
  152.                     break;
  153.                 case kStdClose:
  154.                     if (IsAppWindow(window)) {
  155.                         window = FrontWindowOfType(kwIsDocument, true);
  156.                         if (window)
  157.                             DisposeOneWindow(window, kClose);
  158.                     }
  159.                     else
  160.                         DisposeOneWindow(window, kClose);        /* Dispose of DA window. */
  161.                     break;
  162.                 case kStdSave:
  163.                 case kStdSaveAs:
  164.                     saveMode = (menuItem == kStdSave) ? kSave : kSaveAs;
  165.                     if ((*frHndl)->fileState.refNum == kInvalRefNum)
  166.                         saveMode = kSaveAs;
  167.                     err = SaveDocument(frHndl, window, saveMode);
  168.                     if ((err) && (err != userCanceledErr)) {
  169.                         gDialogErr = err;
  170.                         NewDocumentWindow(nil, 'ERR#', false);
  171.                     }
  172.                     break;
  173.                 case kStdPageSetup:
  174.                     DoSetCursor(&qd.arrow);
  175.                     PresentStyleDialog(frHndl);
  176.                     break;
  177.                 case kStdPrint:
  178.                     DoSetCursor(&qd.arrow);
  179.                     err = noErr;
  180.                     if (!(*frHndl)->d.doc.fhInfo.printRecValid)
  181.                         err = PresentStyleDialog(frHndl);
  182.                     if (!err) {
  183.                         err = PrintDocument(frHndl, true, true);
  184.                         PrintDocument(nil, false, false);
  185.                     }
  186.                     if ((err) && (err != userCanceledErr)) {
  187.                         gDialogErr = err;
  188.                         NewDocumentWindow(nil, 'ERR#', false);
  189.                     }
  190.                     break;
  191.                 case kStdQuit:
  192.                     gQuitApplication = DisposeAllWindows();
  193.                     break;
  194.                 default:
  195.                     handled = false;
  196.                     break;
  197.             }
  198.             break;
  199.  
  200.         case mEdit:            /* Call SystemEdit for DA editing & MultiFinder. */
  201. #if VH_VERSION
  202.             if (menuItem == kStdViewHier) {
  203.                 handled = false;
  204.                 break;
  205.             }
  206. #endif
  207.             if (IsAppWindow(window)) {
  208.                 switch (menuItem) {
  209.                     case kStdUndo:
  210.                     case kStdRedo:
  211.                     case kStdCut:
  212.                     case kStdCopy:
  213.                     case kStdPaste:
  214.                     case kStdClear:
  215.                         switch ((*frHndl)->fileState.sfType) {
  216.                                 /* This is written with the assumption that document types
  217.                                 ** that demand specific code will be added.  The below “if”
  218.                                 ** illustrates how to handle the edit menu for windows that
  219.                                 ** have an active TextEdit control.  The “else” shows a typical
  220.                                 ** undo/redo scenario for applications that are using the
  221.                                 ** hierarchical document package.  The clipboard features
  222.                                 ** are of course document-dependent, so a sample hasn't been
  223.                                 ** implemented here.  For a sample, see DTS.Draw. */
  224.                             default:
  225.                                 te = CTEFindActive(window);
  226.                                 if (te) {
  227.                                     if ((*te)->viewRect.left < -8192)
  228.                                         BeginFrame(window);
  229.                                     else
  230.                                         BeginContent(window);
  231.                                     if (menuItem == kStdUndo)
  232.                                         CTEUndo();
  233.                                     else
  234.                                         CTEClipboard(menuItem - kStdCut + 2);
  235.                                     EndContent(window);
  236.                                 }
  237.                                 else {
  238.                                     if (menuItem <= kStdRedo) {
  239.                                         if (!UnmapMItem(mEdit, kStdUndo)) {
  240.                                             GetUndoInfo(frHndl, &undoDepth, &numUndos);
  241.                                             DoUndoTask((*frHndl)->d.doc.root, 1 - undoDepth, true);
  242.                                         }
  243.                                         else DoUndoTask((*frHndl)->d.doc.root, menuItem - kStdUndo, true);
  244.                                     }
  245.                                     else {
  246.                                         /* Handle rest of edit menu here. */
  247.                                     }
  248.                                 }
  249.                                 break;
  250.                         }
  251.                         break;
  252.                 }
  253.             }
  254.             else SystemEdit(menuItem - 1);
  255.             break;
  256.  
  257.         default:
  258.             handled = false;
  259.             break;
  260.  
  261.     }
  262.  
  263.     return(handled);
  264. }
  265.  
  266.  
  267.  
  268. /*****************************************************************************/
  269.  
  270.  
  271.  
  272. #pragma segment Menu
  273. Boolean    DoAdjustFileMenu(WindowPtr window)
  274. {
  275.     MenuHandle    menu;
  276.     FileRecHndl    frHndl;
  277.     short        enableItem;
  278.  
  279.     menu = GetMenuHandle(mFile);
  280.     EnableItem(menu, UnmapMItem(mFile, kStdQuit));            /* Gotta be able to quit. */
  281.  
  282.     if (IsDAWindow(window)) {
  283.         EnableItem(menu, UnmapMItem(mFile, kStdClose));        /* Let DAs do a close from the menu. */
  284.         return(false);
  285.     }
  286.  
  287.     if (!gLowOnMem) {
  288.         EnableItem(menu, UnmapMItem(mFile, kStdNew));
  289.         EnableItem(menu, UnmapMItem(mFile, kStdOpen));
  290.     }
  291.  
  292.     window = FrontWindowOfType(kwIsDocument, true);
  293.     if (window) {
  294.         EnableItem(menu, UnmapMItem(mFile, kStdClose));
  295.         frHndl = (FileRecHndl)GetWRefCon(window);
  296.         if ((*frHndl)->fileState.sfType == kDocFileType) {
  297.             enableItem = GetWindowDirty(window);
  298.             if ((*frHndl)->fileState.refNum == kInvalRefNum)
  299.                 enableItem = true;
  300.             if (enableItem)
  301.                 EnableItem(menu, UnmapMItem(mFile, kStdSave));
  302.             EnableItem(menu, UnmapMItem(mFile, kStdSaveAs));
  303.         }
  304.         EnableItem(menu, UnmapMItem(mFile, kStdPageSetup));
  305.         EnableItem(menu, UnmapMItem(mFile, kStdPrint));
  306.     }
  307.  
  308.     return(false);
  309. }
  310.  
  311.  
  312.  
  313. /*****************************************************************************/
  314.  
  315.  
  316.  
  317. #pragma segment Menu
  318. Boolean    DoAdjustEditMenu(WindowPtr window)
  319. {
  320.     MenuHandle        menu;
  321.     Boolean            menuEnabled;
  322.     FileRecHndl        frHndl;
  323.  
  324.     menu = GetMenuHandle(mEdit);
  325.  
  326.     if (IsDAWindow(window)) {
  327.         EnableItem(menu, UnmapMItem(mEdit, kStdUndo));
  328.         EnableItem(menu, UnmapMItem(mEdit, kStdCut));
  329.         EnableItem(menu, UnmapMItem(mEdit, kStdCopy));
  330.         EnableItem(menu, UnmapMItem(mEdit, kStdPaste));
  331.         EnableItem(menu, UnmapMItem(mEdit, kStdClear));
  332.         return(false);
  333.     }
  334.  
  335.     if (IsAppWindow(window)) {
  336.         frHndl = (FileRecHndl)GetWRefCon(window);
  337.         switch ((*frHndl)->fileState.sfType) {
  338. #if VH_VERSION
  339.             case kViewHierFileType:
  340.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  341.                                                  UnmapMItem(mEdit, kStdCut));
  342.                 break;
  343. #endif
  344.             default:
  345. #if VH_VERSION
  346.                 EnableItem(menu, UnmapMItem(mEdit, kStdViewHier));
  347. #endif
  348.                 CTEEditMenu(&menuEnabled, mEdit, UnmapMItem(mEdit, kStdUndo),
  349.                                                  UnmapMItem(mEdit, kStdCut));
  350.                 break;
  351.         }
  352.     }
  353.  
  354.     return(false);
  355. }
  356.  
  357.  
  358.  
  359.